package com.ruoyi.service;

import com.ruoyi.bean.OrderFrom;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dto.OrderDTO;
import com.ruoyi.entity.BizOrder;
import com.ruoyi.entity.BizReserveOrder;
import com.ruoyi.entity.SxfOrder;
import com.ruoyi.entity.SxfReserveOrder;
import com.ruoyi.mapper.BizOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.param.OrderParam;
import com.sxftech.utils.CopyBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.BizOrderFrom;
import com.sxftech.resp.ResponseData;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 订单主表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-28
 */
@Service
public class BizOrderService extends ServiceImpl<BizOrderMapper, BizOrder> {

	@Autowired
	private BizOrderMapper bizOrderMapper;
	@Autowired
	private BizReserveOrderService bizReserveOrderService;
	@Autowired
	private SxfReserveOrderService sxfReserveOrderService;
	@Autowired
	private SxfOrderService sxfOrderService;

	public ResponseData create(BizOrderFrom bizOrder) {
		BizOrder entity = transformationFrom(bizOrder);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(BizOrderFrom bizOrder) {
		if(findById(bizOrder.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizOrder entity = transformationFrom(bizOrder);
		entity.setId(bizOrder.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public BizOrder transformationFrom(BizOrderFrom bizOrder) {
		BizOrder entity = new BizOrder();
		entity.setOrderSn(bizOrder.getOrderSn());
		entity.setOrderType(bizOrder.getOrderType());
		entity.setOrderDesc(bizOrder.getOrderDesc());
		entity.setMobile(bizOrder.getMobile());
		entity.setPreAmount(bizOrder.getPreAmount());
		entity.setOnlinePaid(bizOrder.getOnlinePaid());
		entity.setOfflinePaid(bizOrder.getOfflinePaid());
		entity.setAmount(bizOrder.getAmount());
		entity.setActualAmount(bizOrder.getActualAmount());
		entity.setMerchantDiscountAmount(bizOrder.getMerchantDiscountAmount());
		entity.setPlatformDiscountAmount(bizOrder.getPlatformDiscountAmount());
		entity.setDiscountAmount(bizOrder.getDiscountAmount());
		entity.setIntegral(bizOrder.getIntegral());
		entity.setPriceSystemCode(bizOrder.getPriceSystemCode());
		entity.setPayStatus(bizOrder.getPayStatus());
		entity.setPayExpireTime(bizOrder.getPayExpireTime());
		entity.setPaySerialNumber(bizOrder.getPaySerialNumber());
		entity.setPrePayTime(bizOrder.getPrePayTime());
		entity.setPayTime(bizOrder.getPayTime());
		entity.setCancelTime(bizOrder.getCancelTime());
		entity.setOrderStatus(bizOrder.getOrderStatus());
		entity.setInvoiceStatus(bizOrder.getInvoiceStatus());
		entity.setReservePaySn(bizOrder.getReservePaySn());
		entity.setPropertyWithdrawChannel(bizOrder.getPropertyWithdrawChannel());
		entity.setLeavePaySn(bizOrder.getLeavePaySn());
		entity.setAppId(bizOrder.getAppId());
		entity.setPayAppId(bizOrder.getPayAppId());
		entity.setWithdrawChannel(bizOrder.getWithdrawChannel());
		entity.setPayChannel(bizOrder.getPayChannel());
		entity.setClearingStatus(bizOrder.getClearingStatus());
		entity.setCreateTime(bizOrder.getCreateTime());
		entity.setModifyTime(bizOrder.getModifyTime());
		return entity;
	}

	public BizOrderFrom transformationBean(BizOrder bizOrder) {
		BizOrderFrom from = new BizOrderFrom();
		from.setOrderSn(bizOrder.getOrderSn());
		from.setOrderType(bizOrder.getOrderType());
		from.setOrderDesc(bizOrder.getOrderDesc());
		from.setMobile(bizOrder.getMobile());
		from.setPreAmount(bizOrder.getPreAmount());
		from.setOnlinePaid(bizOrder.getOnlinePaid());
		from.setOfflinePaid(bizOrder.getOfflinePaid());
		from.setAmount(bizOrder.getAmount());
		from.setActualAmount(bizOrder.getActualAmount());
		from.setMerchantDiscountAmount(bizOrder.getMerchantDiscountAmount());
		from.setPlatformDiscountAmount(bizOrder.getPlatformDiscountAmount());
		from.setDiscountAmount(bizOrder.getDiscountAmount());
		from.setIntegral(bizOrder.getIntegral());
		from.setPriceSystemCode(bizOrder.getPriceSystemCode());
		from.setPayStatus(bizOrder.getPayStatus());
		from.setPayExpireTime(bizOrder.getPayExpireTime());
		from.setPaySerialNumber(bizOrder.getPaySerialNumber());
		from.setPrePayTime(bizOrder.getPrePayTime());
		from.setPayTime(bizOrder.getPayTime());
		from.setCancelTime(bizOrder.getCancelTime());
		from.setOrderStatus(bizOrder.getOrderStatus());
		from.setInvoiceStatus(bizOrder.getInvoiceStatus());
		from.setReservePaySn(bizOrder.getReservePaySn());
		from.setPropertyWithdrawChannel(bizOrder.getPropertyWithdrawChannel());
		from.setLeavePaySn(bizOrder.getLeavePaySn());
		from.setAppId(bizOrder.getAppId());
		from.setPayAppId(bizOrder.getPayAppId());
		from.setWithdrawChannel(bizOrder.getWithdrawChannel());
		from.setPayChannel(bizOrder.getPayChannel());
		from.setClearingStatus(bizOrder.getClearingStatus());
		from.setCreateTime(bizOrder.getCreateTime());
		from.setModifyTime(bizOrder.getModifyTime());

		return from;
	}

	
	public BizOrderFrom findById(String id) {
		BizOrder entity = this.getOne(new QueryWrapper<BizOrder>().lambda()
				.eq(BizOrder::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizOrder bizOrder = new BizOrder();
		bizOrder.setId(id);
		this.updateById(bizOrder);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, BizOrderFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<BizOrder>().lambda()
				.eq(StringUtils.isNotEmpty(ibean.getOrderSn()), BizOrder::getOrderSn, ibean.getOrderSn())
				.eq(StringUtils.isNotEmpty(ibean.getOrderType()), BizOrder::getOrderType, ibean.getOrderType())
				.eq(StringUtils.isNotEmpty(ibean.getOrderDesc()), BizOrder::getOrderDesc, ibean.getOrderDesc())
				.eq(StringUtils.isNotEmpty(ibean.getMobile()), BizOrder::getMobile, ibean.getMobile())
				.eq(ibean.getPreAmount() != null, BizOrder::getPreAmount, ibean.getPreAmount())
				.eq(ibean.getOnlinePaid() != null, BizOrder::getOnlinePaid, ibean.getOnlinePaid())
				.eq(ibean.getOfflinePaid() != null, BizOrder::getOfflinePaid, ibean.getOfflinePaid())
				.eq(ibean.getAmount() != null, BizOrder::getAmount, ibean.getAmount())
				.eq(ibean.getActualAmount() != null, BizOrder::getActualAmount, ibean.getActualAmount())
				.eq(ibean.getMerchantDiscountAmount() != null, BizOrder::getMerchantDiscountAmount, ibean.getMerchantDiscountAmount())
				.eq(ibean.getPlatformDiscountAmount() != null, BizOrder::getPlatformDiscountAmount, ibean.getPlatformDiscountAmount())
				.eq(ibean.getDiscountAmount() != null, BizOrder::getDiscountAmount, ibean.getDiscountAmount())
				.eq(ibean.getIntegral() != null, BizOrder::getIntegral, ibean.getIntegral())
				.eq(StringUtils.isNotEmpty(ibean.getPriceSystemCode()), BizOrder::getPriceSystemCode, ibean.getPriceSystemCode())
				.eq(StringUtils.isNotEmpty(ibean.getPayStatus()), BizOrder::getPayStatus, ibean.getPayStatus())
				.eq(ibean.getPayExpireTime() != null, BizOrder::getPayExpireTime, ibean.getPayExpireTime())
				.eq(StringUtils.isNotEmpty(ibean.getPaySerialNumber()), BizOrder::getPaySerialNumber, ibean.getPaySerialNumber())
				.eq(ibean.getPrePayTime() != null, BizOrder::getPrePayTime, ibean.getPrePayTime())
				.eq(ibean.getPayTime() != null, BizOrder::getPayTime, ibean.getPayTime())
				.eq(ibean.getCancelTime() != null, BizOrder::getCancelTime, ibean.getCancelTime())
				.eq(StringUtils.isNotEmpty(ibean.getOrderStatus()), BizOrder::getOrderStatus, ibean.getOrderStatus())
				.eq(StringUtils.isNotEmpty(ibean.getInvoiceStatus()), BizOrder::getInvoiceStatus, ibean.getInvoiceStatus())
				.eq(StringUtils.isNotEmpty(ibean.getReservePaySn()), BizOrder::getReservePaySn, ibean.getReservePaySn())
				.eq(StringUtils.isNotEmpty(ibean.getPropertyWithdrawChannel()), BizOrder::getPropertyWithdrawChannel, ibean.getPropertyWithdrawChannel())
				.eq(StringUtils.isNotEmpty(ibean.getLeavePaySn()), BizOrder::getLeavePaySn, ibean.getLeavePaySn())
				.eq(ibean.getAppId() != null, BizOrder::getAppId, ibean.getAppId())
				.eq(ibean.getPayAppId() != null, BizOrder::getPayAppId, ibean.getPayAppId())
				.eq(StringUtils.isNotEmpty(ibean.getWithdrawChannel()), BizOrder::getWithdrawChannel, ibean.getWithdrawChannel())
				.eq(StringUtils.isNotEmpty(ibean.getPayChannel()), BizOrder::getPayChannel, ibean.getPayChannel())
				.eq(StringUtils.isNotEmpty(ibean.getClearingStatus()), BizOrder::getClearingStatus, ibean.getClearingStatus())
				.eq(ibean.getCreateTime() != null, BizOrder::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifyTime() != null, BizOrder::getModifyTime, ibean.getModifyTime())
				);
	}

	@DataSource(value = DataSourceType.ORDER)
	public Page<OrderDTO> selectList(OrderParam orderParam){
		Page<OrderDTO> page = new Page<>(orderParam.getPage(),orderParam.getSize());
		Page<OrderDTO> bizOrderPage = bizOrderMapper.selectList(page,orderParam.getPlCode(),orderParam.getSpaceCode(),orderParam.getPlateNumber(),orderParam.getStartTime(),orderParam.getEndTime(),orderParam.getOrderStatus(),orderParam.getOrderNo());
		return bizOrderPage;
	}

	@DataSource(value = DataSourceType.ORDER)
	public OrderDTO getOrderSn(String orderSn){
		return bizOrderMapper.getOrderSn(orderSn);
	}

	@DataSource(value = DataSourceType.ORDER)
	@Transactional
	public BizOrder updateOrder(OrderFrom orderFrom){
		BizOrder bizOrder =  getById(orderFrom.getId());
		if(bizOrder != null){
			bizOrder.setOrderStatus(orderFrom.getOrderStatus());
			SxfOrder sxfOrder = new SxfOrder();
			CopyBeanUtils.copyNonNullPropertites(bizOrder,sxfOrder);
			sxfOrder.setOrderId(bizOrder.getId());
			sxfOrderService.save(sxfOrder);
			updateById(bizOrder);
			List<BizReserveOrder> bizReserveOrders = bizReserveOrderService.list(new QueryWrapper<BizReserveOrder>().lambda().eq(BizReserveOrder::getOrderId,orderFrom.getId()));
			BizReserveOrder bizReserveOrder = bizReserveOrders.get(0);
			if(orderFrom.getLeaveTime() !=null) bizReserveOrder.setLeaveTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",orderFrom.getLeaveTime()));
			else bizReserveOrder.setLeaveTime(null);
			bizReserveOrder.setSpaceProperty(orderFrom.getSpaceProperty());
			SxfReserveOrder sxfReserveOrder = new SxfReserveOrder();
			CopyBeanUtils.copyNonNullPropertites(bizReserveOrder,sxfReserveOrder);
			sxfReserveOrder.setRoId(bizReserveOrder.getId());
			sxfReserveOrderService.save(sxfReserveOrder);
			bizReserveOrderService.updateById(bizReserveOrder);
		}

		return bizOrder;
	}
}
